package com.esender.smsgateway.config;

import com.esender.common.constant.RabbitMQConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    // 声明死信队列,需要准备普通交换机，普通队列，死信交换机，死信队列
    @Bean
    public Exchange normalExchange() {
        return ExchangeBuilder.fanoutExchange(RabbitMQConstants.SMS_GATEWAY_NORMAL_EXCHANGE).build();
    }

    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable(RabbitMQConstants.SMS_GATEWAY_NORMAL_QUEUE)
                .withArgument("x-message-ttl",10000)
                .withArgument("x-dead-letter-exchange",RabbitMQConstants.SMS_GATEWAY_DEAD_EXCHANGE)
                .withArgument("x-dead-letter-routing-key","")
                .build();
    }

    @Bean
    public Binding normalBinding(Exchange normalExchange, Queue normalQueue) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("").noargs();
    }

    @Bean
    public Exchange deadExchange() {
        return ExchangeBuilder.fanoutExchange(RabbitMQConstants.SMS_GATEWAY_DEAD_EXCHANGE).build();
    }

    @Bean
    public Queue deadQueue() {
        return QueueBuilder.durable(RabbitMQConstants.SMS_GATEWAY_DEAD_QUEUE).build();
    }

    @Bean
    public Binding deadBinding(Exchange deadExchange, Queue deadQueue) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("").noargs();
    }
}
